#include"draw_result.h"

DrawResult* DrawResult_ = NULL;

DrawResult::DrawResult(GroundData* GroundData_, PipeData* PipeData_) {
  this->PipeData_ = PipeData_;
  this->GroundData_ = GroundData_;
  min_temperature = 287.0f;
  max_temperature = 303.0f;
  partition_width = 1;
  return;
}

void DrawResult::Draw() {
  coef_x = window_size_x/(2.0f*(GroundData_->width + PipeData_->delta_x_inside
    + PipeData_->delta_x_outside));
  coef_y = window_size_y/GroundData_->depth;
  DrawPipe();
  DrawGround();
  DrawPartition();
  return;
}

void DrawResult::DrawFillRestangle(float left, float top, float right, float bottom, int r, int g, int b) {
  if((left > right) || (top > bottom))
    return;
  glColor3f(r/255.0f, g/255.0f, b/255.0f);
  glBegin(GL_POLYGON);
  glVertex2f(left, top);
  glVertex2f(right, top);
  glVertex2f(right, bottom);
  glVertex2f(left, bottom);
  glEnd();
}

void DrawResult::DrawPipe(void) {
  HPEN Pen;
  float part;
  for(int i=0; i<PipeData_->cell_q_y; i++) {
    part = (PipeData_->T_outside[i] - min_temperature)/(max_temperature-min_temperature);
    DrawFillRestangle(window_size_x/2.0f-PipeData_->delta_x_outside*coef_x-PipeData_->delta_x_inside*coef_x,
      PipeData_->delta_y*i*coef_y,
      window_size_x/2.0f-PipeData_->delta_x_inside*coef_x,
      PipeData_->delta_y*(i+1)*coef_y, (int)(255.0f*(part)),0,(int)(255.0f*(1.0f-part)));
  }
  for(int i=0; i<PipeData_->cell_q_y; i++) {
    part = (PipeData_->T_inside[i] - min_temperature)/(max_temperature-min_temperature);
    DrawFillRestangle(window_size_x/2.0f-PipeData_->delta_x_inside*coef_x,
      PipeData_->delta_y*i*coef_y,
      window_size_x/2.0f,
      PipeData_->delta_y*(i+1)*coef_y, (int)(255.0f*(part)),0,(int)(255.0f*(1.0f-part)));
  }

  for(int i=0; i<PipeData_->cell_q_y; i++) {
    part = (PipeData_->T_outside[i] - min_temperature)/(max_temperature-min_temperature);
    DrawFillRestangle(window_size_x/2.0f+PipeData_->delta_x_inside*coef_x,
      PipeData_->delta_y*i*coef_y,
      window_size_x/2.0f+PipeData_->delta_x_outside*coef_x+PipeData_->delta_x_inside*coef_x,
      PipeData_->delta_y*(i+1)*coef_y, (int)(255.0f*(part)),0,(int)(255.0f*(1.0f-part)));
  }
  for(int i=0; i<PipeData_->cell_q_y; i++) {
    part = (PipeData_->T_inside[i] - min_temperature)/(max_temperature-min_temperature);
    DrawFillRestangle(window_size_x/2.0f,
      PipeData_->delta_y*i*coef_y,
      window_size_x/2.0f+PipeData_->delta_x_inside*coef_x,
      PipeData_->delta_y*(i+1)*coef_y, (int)(255.0f*(part)),0,(int)(255.0f*(1.0f-part)));
  }
  return;
}

void DrawResult::DrawGround(void) {
  float part;
  float start_ground_x = window_size_x/2.0f-PipeData_->delta_x_outside*coef_x-PipeData_->delta_x_inside*coef_x;
  float start_now_x = 0.0f;
  for(int i=0; i<GroundData_->cell_q_x; i++) {
    for(int j=0; j<GroundData_->cell_q_y; j++) {
      part = (GroundData_->T[i][j] - min_temperature)/(max_temperature-min_temperature);
      DrawFillRestangle(start_ground_x - start_now_x*coef_x - GroundData_->delta_x[i]*coef_x,
        GroundData_->delta_y*j*coef_y,
        start_ground_x - start_now_x*coef_x,
        GroundData_->delta_y*(j+1)*coef_y, (int)(255.0f*(part)),0,(int)(255.0f*(1.0f-part)));
    }
    start_now_x += GroundData_->delta_x[i];
  }
  start_ground_x = window_size_x/2.0f + PipeData_->delta_x_outside*coef_x + PipeData_->delta_x_inside*coef_x;
  start_now_x = 0.0f;
  for(int i=0; i<GroundData_->cell_q_x; i++) {
    for(int j=0; j<GroundData_->cell_q_y; j++) {
      part = (GroundData_->T[i][j] - min_temperature)/(max_temperature-min_temperature);
      DrawFillRestangle(start_ground_x + start_now_x*coef_x,
        GroundData_->delta_y*j*coef_y,
        start_ground_x + start_now_x*coef_x + GroundData_->delta_x[i]*coef_x,
        GroundData_->delta_y*(j+1)*coef_y, (int)(255.0f*(part)),0,(int)(255.0f*(1.0f-part)));
    }
    start_now_x += GroundData_->delta_x[i];
  }
  return;
}

void DrawResult::DrawPartition(void) {
  float start_part_x = window_size_x/2.0f - PipeData_->delta_x_inside*coef_x;
  DrawFillRestangle(start_part_x, 0, start_part_x+partition_width, GroundData_->delta_y*GroundData_->cell_q_y*coef_y, 
    0, 0, 0);
  start_part_x = window_size_x/2.0f + PipeData_->delta_x_inside*coef_x;
  DrawFillRestangle(start_part_x, 0, start_part_x+partition_width, GroundData_->delta_y*GroundData_->cell_q_y*coef_y, 
    0, 0, 0);
  start_part_x = window_size_x/2.0f - PipeData_->delta_x_inside*coef_x - PipeData_->delta_x_outside*coef_x;
  DrawFillRestangle(start_part_x, 0, start_part_x+partition_width, GroundData_->delta_y*GroundData_->cell_q_y*coef_y, 
    0, 0, 0);
  start_part_x = window_size_x/2.0f + PipeData_->delta_x_inside*coef_x + PipeData_->delta_x_outside*coef_x;
  DrawFillRestangle(start_part_x, 0, start_part_x+partition_width, GroundData_->delta_y*GroundData_->cell_q_y*coef_y, 
    0, 0, 0);
  return;
}
